package com.forsrc.utils;

/* 
 * Licensed to the Apache Software Foundation (ASF) under one or more 
 * contributor license agreements.  See the NOTICE file distributed with 
 * this work for additional information regarding copyright ownership. 
 * The ASF licenses this file to You under the Apache License, Version 2.0 
 * (the "License"); you may not use this file except in compliance with 
 * the License.  You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */  
 
import java.io.File;
import java.io.IOException;

public class DirUtils {

    public static void file(final File file, final DirHandle dirHandle, final FileHandle fileHandle) throws HandleException {

        if (file.isFile()) {
            fileHandle.handle(file);
            return;
        }
        dirHandle.handle(file);
        File[] files = file.listFiles();
        if (files == null) {
            return;
        }
        for (File currentFile : files) {
            if (currentFile.isFile()) {
                fileHandle.handle(currentFile);
                continue;
            }
            if (currentFile.isDirectory()) {
                file(currentFile, dirHandle, fileHandle);
            }
        }

    }

    public static void file(final String fileName, final DirHandle dirHandle, final FileHandle fileHandle) throws HandleException {
        File file = new File(fileName);
        if (file.isFile()) {
            fileHandle.handle(file);
            return;
        }
        dirHandle.handle(file);
        String[] files = file.list();
        if (files == null) {
            return;
        }
        for (String currentFileName : files) {
            File currentFile = new File(file.getAbsolutePath() + File.separator + currentFileName);
            if (currentFile.isFile()) {
                fileHandle.handle(currentFile);
                continue;
            }
            if (currentFile.isDirectory()) {
                file(currentFile.getAbsolutePath(), dirHandle, fileHandle);
            }
        }

    }

    public static void dir(final File dir, final DirHandle dirHandle, final FileHandle fileHandle) throws HandleException {
        if (dir.isFile()) {
            fileHandle.handle(dir);
            return;
        }
        dirHandle.handle(dir);
        File[] files = dir.listFiles();
        if (files == null) {
            return;
        }
        for (File currentFile : files) {
            if (currentFile.isFile()) {
                fileHandle.handle(currentFile);
                continue;
            }
            if (currentFile.isDirectory()) {
                dirHandle.handle(currentFile);
                dir(currentFile.listFiles(), dirHandle, fileHandle);
            }
        }

    }

    public static void dir(File[] dirList, final DirHandle dirHandle, final FileHandle fileHandle) throws HandleException {
        if (dirList == null) {
            return;
        }
        for (File currentFile : dirList) {
            if (currentFile.isFile()) {
                fileHandle.handle(currentFile);
                continue;
            }
            if (currentFile.isDirectory()) {
                dirHandle.handle(currentFile);
                dir(currentFile.listFiles(), dirHandle, fileHandle);
            }
        }
    }

    public static interface DirHandle {
        public void handle(final File dir) throws HandleException;
    }

    public static interface FileHandle {
        public void handle(final File file) throws HandleException;
    }

    public static class HandleException extends IOException {

    }

}